package com.yy.yyui.table
{
	
	import com.yy.tools.arry.ArrayTools;
	import com.yy.tools.graphics.GT;
	import com.yy.yyui.EmptyDisplayObject;
	import com.yy.yyui.IActionList;
	import com.yy.yyui.IGrid;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class DataGrid extends Sprite implements IActionList,IGrid
	{
		private var _actions:Object;
		private var _defaultCellWidth:Number;//默认单元格宽
		private var _defaultCellHeight:Number;//默认单元格搞
		
		private var _cellWidths:Array;
		private var _cellHeights:Array;
		private var _gap:Number;
		
		//column config		
		private var _dataFields:Array;
		private var _columnLabels:Array;
		
		//panel		
		private var _bgPanel:Sprite;
		private var _contentPanel:Sprite;
		private var _linePanel:Sprite;
		
		//cell parts
		private var _bgs:Array;
		private var _cell:Array;
		private var _positions:Array;
		
		//misc
		private var _tween:Boolean;
		
		//mouse effect
		private var selectedRowBackground:Object;
		private var _enableRowMouseOver:Boolean;
		private var _enableRowMouseSelect:Boolean;
		
		
		public static const DATAGRID_UPDATE:String = "datagridupdate";
		
		public static const CENTER:Object = "center";
		public static const LEFT:Object = "left";
		public static const RIGHT:Object = "right";
		
		
		public var selectRowFunc:Function;
		public var moveRowFunc:Function;
		private var labelColor:Object;
		public function DataGrid(defaultCellWidth:Number,defaultCellHeight:Number = 20,gap:Number = 1,labelColor:Object = null)
		{
			//TODO: implement function
			super();
			addChild(new EmptyDisplayObject(10,20));
			this.labelColor = labelColor;
			//set widths and heights
			this._defaultCellWidth = defaultCellWidth;
			this._defaultCellHeight = defaultCellHeight;
			this._gap = gap;
			
			this._cellWidths = new Array();
			this._cellHeights = new Array();
			
			//initialize panels
			var sp:Sprite;
			
			sp = new Sprite();
			this.addChild(sp);
			this._bgPanel = sp;
			
			sp = new Sprite();
			this.addChild(sp);
			this._linePanel = sp;
			
			sp = new Sprite();
			this.addChild(sp);
			this._contentPanel = sp;
			
			this._bgs = new Array();
			this._cell = new Array();
			this._positions = new Array();
			
			//column config
			this._dataFields = new Array();
			this._columnLabels = new Array();
			
			this.tween = false;
			
			this.enableRowMouseOver =false;
			this.enableRowMouseSelect =false;
			
			//set actions
			_actions = {};
			
			this.addEventListener(MouseEvent.MOUSE_MOVE,onRowOver);
			this.addEventListener(MouseEvent.CLICK,onRowClick);
			
			this.addEventListener(Event.REMOVED_FROM_STAGE,destroy);
		}
		
		public function destroy(...rest):void{
			this.removeEventListener(Event.REMOVED_FROM_STAGE,destroy);
			this.removeEventListener(MouseEvent.MOUSE_MOVE,onRowOver);
			this.removeEventListener(MouseEvent.CLICK,onRowClick);			
			GT.destroyActions(this.actions);
			GT.removeAllChild(this);
			selectRowFunc = null;
			moveRowFunc = null;
		}
		
		public function get actions():Object{return this._actions;}
		//widths and heights
		public function get defaultCellHeight():Number{return this._defaultCellHeight;}
		public function get defaultCellWidth():Number{return this._defaultCellWidth;}
		public function get gap():Number{return this._gap;}
		public function get cellWidths():Array{return this._cellWidths;}
		public function get cellHeights():Array{return this._cellHeights}
		//parts
		public function get bgPanel():DisplayObjectContainer{return this._bgPanel;}
		public function get contentPanel():DisplayObjectContainer{return this._contentPanel;}
		public function get cells():Array{return this._cell;}
		public function get bgs():Array{return this._bgs;}
		public function get cellPositions():Array{return this._positions;};
		//column config
		public function get dataFields():Array{return this._dataFields};
		public function get columnLabels():Array{return this._columnLabels;}
		
		public function get tween():Boolean{return this._tween;}
		public function set tween(value:Boolean):void{this._tween = value;}
		
		
		public function getCellWidth(index:uint):Number{
			var cw:Object = this.cellWidths[index] as Number;
			return cw==null?this.defaultCellWidth:(cw as Number);
		}
		
		public function getCellHeight(index:uint):Number{
			var ch:Object = this.cellHeights[index];
			return ch==null?this.defaultCellHeight:(ch as Number);
		}
		
		public function getColumnLabel(index:uint):String{
			var obj:Object = this.columnLabels[index];
			return obj==null?"":obj.toString();
		}
		
		public function getCellLocations(rows:uint,columns:uint):Array{
			var i:uint,j:uint;
			var currentX:Number,currentY:Number;
			var cellH:Number,cellW:Number;
			var locationArray:Array = new Array();
			
			currentX = currentY = 0;
			for(i = 0;i<rows;i++){
				currentX = 0;
				cellH = this.getCellHeight(i);
				for(j=0;j<columns;j++){
					cellW = this.getCellWidth(j);
					ArrayTools.setValue(locationArray, new Rectangle(currentX,currentY,cellW,cellH),i,j);
					currentX += (cellW + gap);
				}
				currentY += (cellH + gap);
			}
			return locationArray;
		}
		
		public function get columnLocations():Array{
			var arr:Array = new Array();
			var l:uint = this.columnNum;
			var i:uint = 0;
			var rectX:Number = 0;
			for(;i<l;i++){
				var cw:Number = this.getCellWidth(i);
				var rect:Rectangle = new Rectangle(rectX,0,cw,1);
				rectX += rect.width+this.gap;
				arr[i] = rect;
			}
			return arr;
		}
		
		public function get cellLocations():Array{
			return this.getCellLocations(rowNum,columnNum);
		}
		
		public function get rowNum():uint{
			return Math.max(this.cells.length,this.cellHeights.length,this._bgs.length);
		}
		
		public function get columnNum():uint{
			return Math.max(ArrayTools.get2DimensionsArrayWidth(this.cells),
				this.dataFields.length,this.cellWidths.length,this.columnLabels.length);
		}
		
		
		
		public function setCellWidth(value:Number,index:uint):void{
			this.cellWidths[index] = value;
		}
		
		public function setCellHeight(value:Object,index:uint):void{
			this.cellHeights[index] = value;
			ArrayTools.trim(this.cellHeights);
		}
		
		public function setDataField(value:Object,column:uint):void{
			this.dataFields[column] = value;
			ArrayTools.trim(this.dataFields);
		}
		
		
		public function setColumnLabel(label:Object,column:uint):void{
			this.columnLabels[column] = label;
			ArrayTools.trim(this.columnLabels);
		}
		
		
		public function setRowBackground(row:uint,bg:DisplayObject = null):void{
			this.removeRowBackground(row);
			if(bg==null){
				//默认额 背景 未设置
			}
			this.bgs[row] = bg;
		}
		
		
		private function onRowOver(e:MouseEvent):void{
			if(!this.enableRowMouseOver&&this.moveRowFunc==null) return;
			var i:uint,l:uint;
			var sx:Number = e.stageX;
			var sy:Number = e.stageY;
			var moveFuncKet:Boolean = false;
			for(i = 0,l=this.bgPanel.numChildren;i<l;i++){
				var dobj:DisplayObject = this.bgPanel.getChildAt(i);
				if(selectedRowBackground!=dobj){
					if(dobj.hitTestPoint(sx,sy,true)){
						if(this.enableRowMouseOver)
						{
//							dobj.filters = GlobalConfig.TABLE_ROW_HOVER_FILTER;
						}else
						{
							if(this.moveRowFunc!=null&&!moveFuncKet)
							{
								moveFuncKet = true;
								this.moveRowFunc(i);
							}
						}
					}else{
						dobj.filters = null;
						if(!moveFuncKet)
						{
							if(this.moveRowFunc!=null)
							{
								this.moveRowFunc(this.selectedRow);
							}
						}
					}
				}
			}
			if(this.rowNum == 1)
			{
				if(this.moveRowFunc!=null)
				{
					this.moveRowFunc(i-1);
				}
			}
			e.updateAfterEvent();
		}
		
		private function onRowClick(e:MouseEvent):void{
			if(!this.enableRowMouseSelect&&selectRowFunc == null) return;
			var i:uint,l:uint;
			var sx:Number = e.stageX;
			var sy:Number = e.stageY;			
			for(i = 0,l=this.bgPanel.numChildren;i<l;i++){
				var dobj:DisplayObject = this.bgPanel.getChildAt(i);
				if(dobj.hitTestPoint(sx,sy,true)){
					//选中后的效果
//					dobj.filters = GlobalConfig.TABLE_ROW_HOVER_FILTER;
					selectedRowBackground = dobj;
					if(selectRowFunc != null)
					{
						selectRowFunc(this.selectedRow);
						if(!this.enableRowMouseSelect)
						{
							dobj.filters = null;
						}
					}else
					{
//						dobj.filters = GlobalConfig.TABLE_ROW_HOVER_FILTER;
						selectedRowBackground = dobj;
					}
				}else{
					dobj.filters = null;
				}
			}
		}
		
		public function get selectedRow():int{
			if(selectedRowBackground==null) return -1;
			for(i = 0,l=this.bgPanel.numChildren;i<l;i++){
				var i:uint,l:uint;
				var dobj:DisplayObject = this.bgPanel.getChildAt(i);
				if(dobj==this.selectedRowBackground){
					return i;
				}
			}
			return -1;
		}
		
		public function get enableRowMouseOver():Boolean{return this._enableRowMouseOver;}
		public function get enableRowMouseSelect():Boolean{return this._enableRowMouseSelect;}
		
		public function set enableRowMouseOver(value:Boolean):void{
			var oldValue:Boolean = this.enableRowMouseOver;
			this._enableRowMouseOver = value;
			if(oldValue&&!value){
				this.clearMouseSelect();
			}
		}
		
		
		public function set enableRowMouseSelect(value:Boolean):void{
			var oldValue:Boolean = this.enableRowMouseSelect;
			this._enableRowMouseSelect = value;
			if(oldValue&&!value){
				this.clearMouseSelect();
			}
		}
		
		public function clearMouseSelect():void{
			var i:int,l:int;
			for(i = 0,l=this.bgPanel.numChildren;i<l;i++){
				this.bgPanel.getChildAt(i).filters = null;
			}				
		}		
		
		
		public function removeRowBackground(row:uint):DisplayObject{
			var obj:Object = this.bgs[row];
			if(obj != null &&(obj is DisplayObject)){
				ArrayTools.replace(this.bgs,null,obj);
				var dobj:DisplayObject = obj as DisplayObject;
				if(this.bgPanel.contains(dobj)){
					return this.bgPanel.removeChild(dobj);
				}
			}
			ArrayTools.trim(this.bgs);
			return null;
		}
		public function removeAllBackground():void{
			ArrayTools.clear(this.bgs);
			clearAllBackground();
		}		
		public function clearAllBackground():void{
			GT.removeAllChild(this.bgPanel);
		}
		
		public function setGrid(value:Object,row:uint,column:uint):void{
			var obj:Object = ArrayTools.getValue(this.cells,row,column);
			if(obj==null||!(obj is Cell)){
				ArrayTools.setValue(this.cells,value,row,column);
			}else{
				var cell:Cell = obj as Cell;
				cell.content = value;
				cell.data = value;
			}
		}
		
		public function getGrid(row:uint,column:uint):Object{
			var obj:Object = ArrayTools.getValue(this.cells,row,column);
			if(obj!=null){
				if(obj is Cell){
					return (obj as Cell).data;
				}else{
					return obj;
				}
			}
			return null;
		}
		
		public function findGrid(value:Object):Point{
			var i:int,j:int,il:int,jl:int;
			jl = this.columnNum;
			il = this.rowNum;
			for(i = 0;i<il;i++){
				for(j=0;j<jl;j++){
					if(this.getGrid(i,j)==value){
						return new Point(j,i);
					}
				}
			}
			return null;			
		}
		
		public function getRowAndColumnByXY(stageX:Number,stageY:Number):Point{
			var i:int,j:int,il:int,jl:int;
			jl = this.columnNum;
			il = this.rowNum;
			var arr:Array = this.cellLocations;
			for(i = 0;i<il;i++){
				for(j=0;j<jl;j++){
					var obj:Object = ArrayTools.getValue(arr,i,j);
					var rect:Rectangle = obj as Rectangle;
					var point:Point = new Point(rect.x,rect.y);
					point = this.localToGlobal(point);
					rect.x = point.x;
					rect.y = point.y;
					if(rect.contains(stageX,stageY)){
						return new Point(j,i);
					}
					//					if(obj!=null&&obj is DisplayObject){
					//						var dobj:DisplayObject = obj as DisplayObject;
					//						DT.t(dobj.getBounds(stage));
					//						if(dobj.getBounds(stage).contains(stageX,stageY)){
					//							return new Point(j,i);
					//						}
					//					}
				}
			}
			return null;
		}
		
		public function removeGrid(row:uint,column:uint):void{
			var obj:Object = ArrayTools.getValue(this.cells,row,column);
			if(obj!=null &&(obj is Cell)){
				var cell:Cell = obj as Cell;
				if(this.contentPanel.contains(cell)){
					this.contentPanel.removeChild(cell);
				}
			}
			ArrayTools.setValue(this.cells,null,row,column);
			ArrayTools.trim2D(this.cells);
		}
		
		public function setRow(value:Array,row:uint):void{
			var i:uint,l:uint;
			for(i = 0,l=value.length;i<l;i++){
				setGrid(value[i],row,i);
			}
		}
		
		public function setColumn(value:Array,column:uint):void{
			var i:uint,l:uint;
			for(i = 0,l=value.length;i<l;i++){
				setGrid(value[i],i,column);
			}			
		}
		
		public function clear():void{
			GT.removeAllChild(contentPanel);
			GT.removeAllChild(_linePanel);
			GT.removeAllChild(bgPanel);
			this.cells.length = 0;// = new Array();
		}
		
		public function clearRow(row:uint):void{
			var i:uint;
			var obj:Object;
			var rows:uint = this.rowNum;
			if(row>=rows) return;
			var columns:uint = this.columnNum;
			if(cells[row] is Array){
				var arr:Array = cells[row] as Array;
				for(i = 0;i<columns;i++){
					obj = arr[i];
					if(obj is DisplayObject){
						var dobj:DisplayObject = obj as DisplayObject;
						if(this.contentPanel.contains(dobj)){
							this.contentPanel.removeChild(dobj);
						}
					}
				}
				arr.length = 0;
			}
		}
		
		public function clearColumn(column:uint):void{
			var i:uint;
			var obj:Object;
			var columns:uint = this.columnNum;
			if(column>=columns) return;
			var rows:uint = this.rowNum;
			for(i = 0;i<rows;i++){
				obj = ArrayTools.getValue(this.cells,i,column);
				if(obj is DisplayObject){
					var dobj:DisplayObject = obj as DisplayObject;
					if(this.contentPanel.contains(dobj)){
						this.contentPanel.removeChild(dobj);
					}
				}
				ArrayTools.setValue(this.cells,null,i,column);
			}
		} 
		
		
		public function insertRow(value:Array,row:uint):void{
			ArrayTools.insert(this.cells,row,value);
			ArrayTools.insert(this.cellHeights,row,defaultCellHeight);
		}
		
		public function deleteRow(row:uint):void{
			var deleteBg:Boolean = true;
			if(row>=this.cells.length){
				deleteBg = false;
			}
			clearRow(row);
			this.cells.splice(row,1);
			this.cellHeights.splice(row,1);
			var obj:Object = this.bgs[this.bgs.length-1];
			if(deleteBg&&obj!=null&& obj is DisplayObject){
				this.bgs.length = this.bgs.length-1;
				var dobj:DisplayObject = obj as DisplayObject;
				if(this.bgPanel.contains(dobj)){
					this.bgPanel.removeChild(dobj);					
				}
			}
			ArrayTools.trim(this.bgs);
		}
		
		public function insertColumn(value:Array,column:uint):void{
			var rows:uint = Math.max(this.rowNum,value.length);
			var i:uint;
			for(i = 0;i<rows;i++){
				if(this.cells[i] == null){
					this.cells[i] = new Array();
				}
				if(this.cells[i] as Array){
					var arr:Array = this.cells[i] as Array;
					ArrayTools.insert(arr,column,value[i]);
				}
			}
			ArrayTools.insert(this.cellWidths,column,defaultCellWidth);
			ArrayTools.insert(this.dataFields,column,null);
			ArrayTools.insert(this.columnLabels,column,null);
		}
		
		public function deleteColumn(column:uint):void{
			var i:uint;
			var obj:Object;
			var columns:uint = this.columnNum;
			if(column>=columns) return;
			var rows:uint = this.rowNum;
			for(i = 0;i<rows;i++){
				obj = this.cells[i];
				if(obj!=null&&obj is Array){
					var arr:Array = obj as Array;
					obj = arr[column];
					if(obj!=null&&obj is DisplayObject){
						var dobj:DisplayObject = obj as DisplayObject;
						if(this.contentPanel.contains(dobj)){
							this.contentPanel.removeChild(dobj);
						}
					}
					arr.splice(column,1);
				}
			}			
			this.cellWidths.splice(column,1);
			this.dataFields.splice(column,1);
			this.columnLabels.splice(column,1);
		}
		
		public function setRowDataProvider(value:Object,row:uint):void{
			var i:uint,l:uint;
			var obj:Object;
			l = this.dataFields.length;
			for(i=0;i<l;i++){
				if(this.dataFields[i]!=null&&value.hasOwnProperty(this.dataFields[i].toString())){
					obj = ArrayTools.getValue(this.cells,row,i);
					var fieldValue:Object = value[this.dataFields[i].toString()];	
					if(fieldValue!=null){
						if(obj!=null && obj is Cell){
							var cell:Cell = obj as Cell;
							cell.content = fieldValue;
							cell.data = fieldValue;
						}
						else{
							ArrayTools.setValue(this.cells,fieldValue,row,i);
						}
					}									
				}
			}
		}
		
		private function setDataProvider(value:Array,background:Boolean = false):void{
			var i:uint,il:uint;
			var obj:Object;
			il = value.length;
			//jl = this.dataFields.length;
			for(i=0;i<il;i++){
				if(value[i]!=null){
					this.setRowDataProvider(value[i],i);
				}
				if(background){
					this.setRowBackground(i);
				}
			}
		}
		
		public function set dataProviderWithDefaultRowBackground(value:Array):void{
			this.setDataProvider(value,true);
		}
		public function get linePanel():Sprite
		{
			return this._linePanel;
		}
		
		public function set dataProvider(value:Array):void{
			this.setDataProvider(value,false);
		}
		private var _showLine:Boolean;
		public function set showLine(value:Boolean):void
		{
			this._showLine = value;
		}
		public function get showLine():Boolean
		{
			return this._showLine;
		}
		
		//线条颜色
		private var _lineColor:Object; //size, color, _lineColor.alpha
		public function get lineColor():Object
		{
			return _lineColor;
		}
		
		public function set lineColor(value:Object):void
		{
			_lineColor = value;
		}
		public function get dgWidth():Number
		{
			var tempCount:Number = 0;
			for each(var cW:Number in this.cellWidths)
			{
				tempCount+=cW; 
			}
			return tempCount;
		}
		public function update():void{
			var obj:Object;
			var dobj:DisplayObject;
			var cell:Cell;
			var rect:Rectangle;
			var cl:Array = this.cellLocations;
			var i:uint,j:uint,il:uint,jl:uint;
			var rowWidth:Number,rowHeight:Number;
			var rowY:Number;
			i = j = 0;
			il = this.rowNum;
			jl = this.columnNum;
			for(i=0;i<il;i++){
				rowWidth = 0;
				for(j=0;j<jl;j++){
					rect = cl[i][j] as Rectangle;
					obj = ArrayTools.getValue(this.cells,i,j);
					if(obj!=null){
						if(obj is Cell){
							cell = obj as Cell;
							dobj = cell;
							if(!this.contentPanel.contains(obj as DisplayObject)){
								this.contentPanel.addChild(obj as DisplayObject);
							}
						}else{
							cell = new Cell(rect.width,rect.height,this.labelColor);
							cell.content = obj;
							cell.data = obj;
							this.contentPanel.addChild(cell);
							ArrayTools.setValue(this.cells,cell,i,j);
							dobj = cell;
							dobj.alpha = 0;
							dobj.x = rect.x;
							dobj.y = rect.y+1;
							dobj.width = rect.width;
							dobj.height = rect.height;							
						}
						//var position
						var pos:Object = this.cellPositions[j];
						if(pos==null){
							pos = CENTER;
						}
						cell.position = pos;
						dobj.alpha = 1;
						dobj.x = rect.x;
						dobj.y = rect.y+1;
						dobj.width = rect.width;
						dobj.height = rect.height;							
					}
					rowY = rect.y+1;
					rowWidth = rect.right;
					rowHeight = rect.height;
				}
				if(jl!=0){
					obj = this.bgs[i];
					if(obj!=null && obj is DisplayObject){
						dobj = obj as DisplayObject;
						rect = new Rectangle(0,rowY,rowWidth,rowHeight);
						if(!this.bgPanel.contains(dobj)){
							this.bgPanel.addChild(dobj);
							dobj.x = 2;
							dobj.y = rect.y+1;
							dobj.width = rect.width-2;
							dobj.height = rect.height-2;
						}
						dobj.x = 2;
						dobj.y = rect.y+1;
						dobj.width = rect.width-2;
						dobj.height = rect.height-2;							
					}
				}
			}
			//添加线条
			var tempCount:Number = 0;
			var ic:int;
			if(this.showLine)
			{
				if(this.rowNum > 0)
				{
//					dobj = this._linePanel.addChild(UM.scale9(new BusinessLineBackgroundC()));
//					dobj.filters = GlobalConfig.DARKER_DISABLE_FILTER;
//					dobj.height = this.rowNum*(this.defaultCellHeight+1);
//					dobj.y = 2;
//					
//					dobj = this._linePanel.addChild(UM.scale9(new BusinessLineBackgroundC()));
//					dobj.filters = GlobalConfig.DARKER_DISABLE_FILTER;
//					dobj.height = this.rowNum*(this.defaultCellHeight+1);
//					dobj.x = this.dgWidth-2;
//					dobj.y = 2;
//					for(ic = 0 ; ic < this.rowNum ; ic++)
//					{
//						dobj = this._linePanel.addChild(UM.scale9(new BusinessLineBackground()));
//						dobj.filters = GlobalConfig.DARKER_DISABLE_FILTER;
//						dobj.width = this.dgWidth;
//						dobj.y = ic*(this.defaultCellHeight+1);
//						dobj.x = 1;
//					}
//					dobj = this._linePanel.addChild(UM.scale9(new BusinessLineBackground()));
//					dobj.filters = Sys.DARKER_DISABLE_FILTER;
//					dobj.width = this.dgWidth;
//					dobj.y = (ic)*(this.defaultCellHeight+1);
//					dobj.x = 1;
				}
			}
			if(_lineColor) {
				if(this.rowNum > 0)
				{
					this._linePanel.graphics.clear();
					this.linePanel.graphics.lineStyle(_lineColor.size, _lineColor.color, _lineColor.alpha);
					for(ic = 0 ; ic <= this.rowNum ; ic++)
					{
						this.linePanel.graphics.moveTo(0,ic*(this.defaultCellHeight+1));
						this.linePanel.graphics.lineTo(this.dgWidth, ic*(this.defaultCellHeight+1));
					}
					//					dobj = this._linePanel.addChild(UM.scale9(new BusinessLineBackground()));
					//					dobj.filters = Sys.DARKER_DISABLE_FILTER;
					//					dobj.width = this.dgWidth;
					//					dobj.y = (ic)*(this.defaultCellHeight+1);
					//					dobj.x = 1;
				}
			}
			this.dispatchEvent(new Event(DATAGRID_UPDATE));	
		}
	}
}
import com.yy.tools.graphics.GT;
import com.yy.yyui.IDataContainer;
import com.yy.yyui.table.DataGrid;

import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.text.TextField;

class Cell extends Sprite implements IDataContainer{
	private var w:Number,h:Number;
	private var _content:DisplayObject;
	private var adjust:Function;
	private var _data:Object;
	public function Cell(w:Number=32,h:Number = 32,color:Object = null){
		this.w = w;
		this.h = h;
		adjust = GT.centerScrollChild;
		this.color = color;
		//this.scrollRect = new Rectangle(0,0,this.width,this.height); 
	}
	
	public function set content(value:Object):void{
		var dobj:DisplayObject;
		if(value==null){
			value = GT.createEmptyDisplayObject();
		}
		if(value is DisplayObject){
			dobj = value as DisplayObject;
		}else if(value is Boolean){
			dobj = GT.visualizeBoolean(value as Boolean);
		}else{
			dobj = GT.visualizeString(value,null,1,this.color);
			dobj.cacheAsBitmap = true;
		}
		
		GT.replaceChild(this,dobj,this._content,true,false,false,false);
		this._content = dobj;		
		adjust(this,this._content);
	}
	
	/**
	 *设置单元格的对齐方式 
	 * @param value
	 * 
	 */
	public function set position(value:Object):void{
		switch(value){
			case DataGrid.RIGHT:
				adjust = GT.centerRightScrollChild;
				break;
			case DataGrid.LEFT:
				adjust = GT.centerLeftScrollChild;
				break;
			case DataGrid.CENTER:
			default:
				adjust = GT.centerScrollChild;
		}
		adjust(this,this._content);
	}
	
	override public function get width():Number{
		return w;
	}
	
	override public function get height():Number{
		return h;
	}
	
	override public function set width(newWidth : Number) : void {
		var diff:Number = newWidth - w;
		var oldW:Number,oldH:Number;
		var i:uint,l:uint;
		if(this._content is TextField){
			this._content.width += diff;
		}
		w = newWidth;
		adjust(this,this._content);
	}
	
	
	
	override public function set height(newHeight : Number) : void {
		var diff:Number = newHeight - h;
		var oldW:Number,oldH:Number;
		var i:uint,l:uint;				
		h = newHeight;
		adjust(this,this._content);					
	}
	
	public function set data(value:Object):void{
		this._data = value;
	}	
	
	public function get data():Object{
		return this._data;
	}
	private var _color:Object;
	public function set color(value:Object):void{
		this._color = value;
	}	
	
	public function get color():Object{
		return this._color;
	}
	
}
